﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TransportLayer;
using System.Threading;

namespace TransportLayer.ExecNode
{
    public partial class SinkForm : Form
    {
        private NodeLibrary.Sink sink;
        private NodeLibrary.Caller caller;
        private Thread refresher;
        private bool refloop = true;

        public SinkForm(TransportLayer.Node cNode, int mPort)
        {
            sink = new NodeLibrary.Sink(cNode, mPort);
            caller = new NodeLibrary.Caller(sink.Id, sink.Name);
            sink.CallerListeningPort = caller.Port;
            InitializeComponent();
            refresher = new Thread(RefreshTextBox);
            refresher.Start();
            this.Text = cNode.Name;
            sink.Snp.windowHideHandler += new NodeLibrary.WindowHideHandler(Hide);
            sink.Snp.windowShowHandler += new NodeLibrary.WindowShowHandler(Show);
        }

        private void SinkForm_Load(object sender, EventArgs e)
        {

        }
        private void RefreshTextBox()
        {
            while (refloop)
            {
                Thread.Sleep(50);
                    messageTextBox.Text += sink.Receiver.Buffer;
                callerMessageTextBox.Text = caller.Message;
                sink.Receiver.Buffer = "";
            }
        }

        private void SinkForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            sink.Clean();
            caller.Clean();
        }

        private void logButton_Click(object sender, EventArgs e)
        {
            LogGui log = new LogGui(sink.Log);
            log.Show();
        }

        private void toolsButton_Click(object sender, EventArgs e)
        {
            ToolsGui gui = new ToolsGui();
            gui.Show();
        }

        private void hideButton_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
